package com.ffdms.mobileoffice;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ffdms.service.MessageDigest;
import com.ffdms.service.MessageService;
import com.ffdms.util.MobileOfficeUtil;
import com.ffdms.vo.Staff;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MessageActivity extends BaseActivity {
	private TextView tv_title;
	private Button btn_popup;
	private PopupWindow popupWindow;
	private ListView lv_group;
	private View view;
	private List<String> groups;
	private ListView lv_messsage;
	private MobileOfficeApplication app;
	private MessageService svc1;
	private Staff me;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		app = (MobileOfficeApplication) getApplication();
		me = app.getCurrentStaff();

		populateMessage();
	}

	private void populateMessage() {
		svc1 = new MessageService(this, me);
		lv_messsage = (ListView) findViewById(R.id.lv_message);
		final List<MessageDigest> messages = new ArrayList<MessageDigest>();
		MessageDigest md = new MessageDigest();
		md.setTitle("即时消息");
		md.setContent("王小二：下午的计划做的怎么样了？");
		md.setTime(new Date());
		md.setPhoto(getResources().getDrawable(R.drawable.menu_logistics));
		md.setIntent(new Intent(this, CommunicateActivity.class));
		messages.add(md);

		MessageDigest md2 = new MessageDigest();
		md2.setTitle("系统公告");
		md2.setContent("技术部：计划今晚八点技术部全体会议，务必参加");
		md2.setTime(new Date());
		md2.setPhoto(getResources().getDrawable(R.drawable.menu_logistics));
		md2.setIntent(new Intent(this, NoticeActivity.class));
		messages.add(md2);

		MessageAdapter adp = new MessageAdapter(this, messages);
		lv_messsage.setAdapter(adp);
		lv_messsage.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				MessageDigest m = messages.get(arg2);
				Intent t = m.getIntent();
				t.putExtra("title", m.getTitle());
				t.putExtra("time",
						MobileOfficeUtil.formatDate("yyyy年MM月dd日", m.getTime()));
				t.putExtra("content", m.getContent());
				startActivity(m.getIntent());
			}
		});
	}

	private void showWindow(View parent) {

		if (popupWindow == null) {
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			view = layoutInflater.inflate(R.layout.group_list, null);

			lv_group = (ListView) view.findViewById(R.id.lvGroup);
			groups = new ArrayList<String>();
			groups.add("清空记录");
			groups.add("删除");

			GroupAdapter groupAdapter = new GroupAdapter(this, groups);
			lv_group.setAdapter(groupAdapter);
			popupWindow = new PopupWindow(view, 200, 250);
		}

		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);

		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2
				- popupWindow.getWidth() / 2;

		Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
				+ windowManager.getDefaultDisplay().getWidth() / 2);
		//
		Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);

		Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 0);

		lv_group.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {

				Toast.makeText(MessageActivity.this,
						"groups.get(position)" + groups.get(position), 1000)
						.show();

				if (popupWindow != null) {
					popupWindow.dismiss();
				}
			}
		});
	}

}
